package com.bcgtgjyb.huanwen.customview.mylibrary.spring;

import com.bcgtgjyb.huanwen.customview.mylibrary.tool.ScreenUtil;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bigwen on 2016/4/25.
 */
public class SpringView extends Component implements Component.EstimateSizeListener, Component.DrawTask,
        Component.BindStateChangedListener {

    /**
     * 变量 springH
     */

    public static float springH = 0;
    private static final int FIVE = 5;
    private static final int TWO = 2;
    private static final int TEN = 10;
    private static final int ELEVEN = 11;
    private static final int TWLVE = 12;
    private static final int THIRTEEN = 13;
    private static final int THREE = 3;
    private static final int FOUR = 4;
    private static final int SIX = 6;
    private static final int SEVEN = 7;
    private static final int EIGHT = 8;
    private static final int NINE = 9;
    private static final int ZERO = 0;
    private static final int FOURTEEN = 14;
    private static final int FIFTEEN = 15;
    private static final int FIVEHUNDRED = 500;
    private static final float POINT = 0.125f;
    Point addP = new Point();
    private Context mContext;
    private List<Point> points = new ArrayList<Point>();
    private Paint mPaint;
    private int height,width;
    private int dd;
    private boolean isPlay = false;
    private int moveUnit = 0;
    private Point lastPoint;
    private double degree = 0;
    private Thread mThread;
    private Path path = new Path();

    /**
     * SpringView 构造方法
     *
     * @param context 内容
     */

    public SpringView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    /**
     * SpringView 构造方法
     *
     * @param context 内容
     * @param attrs 属性
     */
    public SpringView(Context context, AttrSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        moveUnit = ScreenUtil.dip2px(mContext, FIVE);
        width = ScreenUtil.getScreenWidth(mContext);
        height = ScreenUtil.getScreenHeight(mContext);
        dd = ScreenUtil.dip2px(mContext, TEN);
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(dd);
        springH = dd;
        addDrawTask(this);
        setEstimateSizeListener(this);
        setBindStateChangedListener(this);
        initPoint();
        initPath();
        initThread();
    }

    private void initPath() {
        path.reset();
        if (points.size() == 0) {
            return;
        }
        for (int ij = 0; ij < points.size(); ij++) {
            if (ij == 0) {
                path.moveTo(points.get(ij).getPointX(), points.get(ij).getPointY());
            } else {
                path.lineTo(points.get(ij).getPointX(), points.get(ij).getPointY());
            }
        }
    }

    private void initPoint() {
        for (int ij = 0; ij < TEN; ij++) {
            Point mPoint = new Point();
            mPoint.modify((float) ((double)width / TWO + ij * dd), (float) ((double)height / TWO));
            points.add(mPoint);
        }
    }

    /**
     * addPoint 方法
     *
     */
    public synchronized void addPoint() {
        points.add(lastPoint);
    }

    /**
     * pointMove 方法
     *
     */
    public synchronized void pointMove() {
        if (points.size() == 0) {
            return;
        }
        lastPoint = points.get(points.size() - 1);
        int xx = (int) (Math.cos(degree) * moveUnit);
        int yy = (int) (Math.sin(degree) * moveUnit);
        Point firstP = points.get(0);
        addP.modify(firstP.getPointX() + xx, firstP.getPointY() + yy);

        points.add(0, addP);
        points.remove(points.size() - 1);
    }

    /**
     * setDegree方法
     *
     * @param ds ds
     */
    public void setDegree(double ds) {
        float pi = (float) Math.PI;
        if (ds < ZERO) {
            ds = ds + pi * TWO;
        }
        float unit = POINT * pi;
        if (ds > FIFTEEN * unit || ds < unit) {
            this.degree = ZERO;
        } else if (ds > unit && ds < THREE * unit) {
            this.degree = TWO * unit;
        } else if (ds > THREE * unit && ds < FIVE * unit) {
            this.degree = FOUR * unit;
        } else if (ds > FIVE * unit && ds < SEVEN * unit) {
            this.degree = SIX * unit;
        } else if (ds > SEVEN * unit && ds < NINE * unit) {
            this.degree = EIGHT * unit;
        } else if (ds > NINE * unit && ds < ELEVEN * unit) {
            this.degree = TEN * unit;
        } else if (ds > ELEVEN * unit && ds < THIRTEEN * unit) {
            this.degree = TWLVE * unit;
        } else if (ds > THIRTEEN * unit && ds < FIFTEEN * unit) {
            this.degree = FOURTEEN * unit;
        }
    }

    private void initThread() {
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isPlay) {
                    pointMove();
                    initPath();
                    invalidate();
                    try {
                        Thread.sleep(FIVEHUNDRED);
                    } catch (Exception e) {
                        e.fillInStackTrace();
                    }
                }
            }
        });
    }

    /**
     * 开始
     *
     */
    private void start() {
        isPlay = true;
        mThread.start();
    }

    /**
     * 停止
     */
    private void stop() {
        isPlay = false;
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        for (int i = 0; i < points.size(); i++) {
            Point pp = points.get(i);
            canvas.drawPoint(pp.getPointX(), pp.getPointY(), mPaint);
        }
    }

    @Override
    public void onComponentBoundToWindow(Component component) {
        start();
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        stop();
    }
}
